use image::{GenericImageView, imageops, Rgb, RgbaImage, RgbImage};

/// 把一个图片，按等分切割成多个图片
#[test]
fn aa() {
    // 加载图片
    let mut img = image::open("1.png").unwrap();

    // 将图像转换为RGB格式
    // let mut img = img.into_rgba8();

    // 获取原始图片的宽度和高度
    let (width, height) = img.dimensions();

    // 计算水平和垂直方向上切割的块数
    let num_chunks_horizontal = 1;
    let num_chunks_vertical = 3;

    // 计算每个切割区域的宽度和高度
    let chunk_width = width / num_chunks_horizontal;
    let chunk_height = height / num_chunks_vertical;

    // 遍历每个切割区域并保存为新图片
    for i in 0..num_chunks_horizontal {
        for j in 0..num_chunks_vertical {
            // 计算当前切割区域的左上角坐标
            let x = i * chunk_width;
            let y = j * chunk_height;

            // 切割图片
            let chunk = imageops::crop(&mut img, x, y, chunk_width, chunk_height).to_image();

            // 保存切割后的图片
            chunk.save(format!("chunk_{}_{}.png", i, j)).unwrap();
        }
    }
}

#[test]
fn bb() {
    // 加载图片
    let mut img = image::open("2.png").unwrap();

    // 将图像转换为RGB格式
    // let mut img = img.into_rgba8();

    // 获取原始图片的宽度和高度
    let (width, height) = img.dimensions();

    // 计算水平和垂直方向上切割的块数
    let num_chunks_horizontal = 1;
    let num_chunks_vertical = 2;

    // 计算每个切割区域的宽度和高度
    let chunk_width = width / num_chunks_horizontal;
    let chunk_height = height / num_chunks_vertical;

    // 遍历每个切割区域并保存为新图片
    for i in 0..num_chunks_horizontal {
        for j in 0..num_chunks_vertical {
            // 计算当前切割区域的左上角坐标
            let x = i * chunk_width;
            let y = j * chunk_height;

            // 切割图片
            let chunk = imageops::crop(&mut img, x, y, chunk_width, chunk_height).to_image();

            // 保存切割后的图片
            chunk.save(format!("chunk22_{}_{}.png", i, j)).unwrap();
        }
    }
}

#[test]
fn cc() {
    // 加载图片
    let mut img = image::open("1.png").unwrap();

    // 将图像转换为灰度图
    let gray_img = img.to_luma8();
    // 获取图像的宽度和高度
    let (width, height) = gray_img.dimensions();
    println!("Image dimensions: {}x{}", width, height);

    let mut output_img = RgbaImage::new(width, height);

    // 遍历图像的每个像素并打印灰度值
    for y in 0..height {
        for x in 0..width {
            let pixel_value = gray_img.get_pixel(x, y)[0];
            // println!("Pixel at ({}, {}): {}", x, y, pixel_value);
            let pixel_value_ori = img.get_pixel(x, y);
            // println!("Pixel at ({}, {}): {:?}", x, y, pixel_value_ori);
            /*if pixel_value > 100 {
                println!("Pixel at ({}, {}): {}", x, y, pixel_value);
            }*/
            // 创建一个新的 RGB 图像，大小与原图相同


            output_img.put_pixel(x, y, pixel_value_ori);
        }
    }

    output_img.save("aadd.png").unwrap();

 /*   // 切割图片
    let chunk = imageops::crop(&mut img, x, y, chunk_width, chunk_height).to_image();

    // 保存切割后的图片
    chunk.save(format!("chunk22_{}_{}.png", i, j)).unwrap();*/

}

#[test]
fn dd() {
    // 加载图片
    let mut img = image::open("1.png").unwrap();

    // 获取图片的宽度和高度
    let (width, height) = img.dimensions();

    // 创建一个新的 RGB 图像，大小与原图相同
    let mut output_img = RgbaImage::new(width, height);

    // 灰度阈值
    let threshold: u8 = 255;

    // 遍历图片的每一个像素
    for y in 0..height {
        for x in 0..width {
            // 获取像素的灰度值
            let pixel = img.get_pixel(x, y);
            let gray_value = (pixel[0] as f32 * 0.2126 + pixel[1] as f32 * 0.7152 + pixel[2] as f32 * 0.0722) as u8;

            // 如果灰度值大于阈值，则保留原像素值；否则将像素设置为黑色
            /*if gray_value == threshold {
                // 在输出图像中设置像素
                output_img.put_pixel(x, y, pixel);
            }*/

            output_img.put_pixel(x, y, pixel);
        }
    }

    // 保存输出图像
    output_img.save("output_image.png").expect("Failed to save image");

}


